#include "CNoteInfoPanel.h"
#include <MBasicLayoutAnimation>
#include <QSwipeGesture>
#include <QGraphicsDropShadowEffect>
#include "QNoteViewWindow.h"

CNoteInfoPanel::CNoteInfoPanel(QGraphicsItem* parent)
    : MWidget(parent), m_hEvernote(QvernoteAPI::Instance()), isExpanded(false), mousePressed(false)
{
    noteInfoPanelLayout = new MLayout(this);
    MBasicLayoutAnimation* layoutAnim = new MBasicLayoutAnimation(noteInfoPanelLayout);
    noteInfoPanelLayoutPolicy = new MGridLayoutPolicy(noteInfoPanelLayout);
    //grabGesture(Qt::SwipeGesture);
    //grabGesture(Qt::PanGesture);
    //setAutoFillBackground(true);
    //QGraphicsOpacityEffect* oe = new QGraphicsOpacityEffect();
    //oe->setOpacity(0.8);
    //setGraphicsEffect(oe);
    setGraphicsEffect(new QGraphicsDropShadowEffect());
    setupUI();

    setZValue(100);
}

QSizeF CNoteInfoPanel::sizeHint ( Qt::SizeHint which, const QSizeF & constraint ) const
{
    return QSizeF(parentWidget()->geometry().width(), 173 + 64);
}

void CNoteInfoPanel::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    QRectF b = boundingRect();
    painter->setOpacity(0.8);
    painter->setBackground(QBrush(Qt::gray));
    painter->fillRect(QRectF(b.x(), b.y(), b.width(), b.height() - INFO_LINE_HEIGHT), Qt::gray);
    painter->setOpacity(1.0);
    painter->setBackground(QBrush(Qt::white));
    QRectF l = lLastTransparentLine.geometry();
    //painter->fillRect(QRectF(b.x(), b.height()- (3*INFO_LINE_HEIGHT / 2), b.width(),  INFO_LINE_HEIGHT),
    //                  Qt::white);
    painter->fillRect(QRectF(b.x(), l.y(), b.width(), l.height()), Qt::white);
}

void CNoteInfoPanel::showEvent( QShowEvent * event)
{
    qDebug() << "CNoteInfoPanel::showEvent" << parentWidget()->geometry().x() << parentWidget()->geometry().y() << parentWidget()->geometry().width();
    QRectF b = QRectF(QPointF(0, 0), sizeHint(Qt::PreferredSize));

    if(parentWidget()->geometry().x() == 0)
    {
        // 90 - the size of header of meego desktop
        setGeometry(QRectF(0, -b.height() + 90 + INFO_LINE_HEIGHT, parentWidget()->geometry().width(), b.height()));
        //animShow();
    }

    animHide();
}

void CNoteInfoPanel::mousePressEvent(QGraphicsSceneMouseEvent* e)
{
    QRectF l = lLastTransparentLine.geometry();

    if(e->pos().x() >= l.x() && e->pos().y() >= l.y() &&
       e->pos().x() <= l.x() + l.width() && e->pos().y() <= l.y() + l.height())
    {
        mousePressed = true;
        e->accept();
    }

}

void CNoteInfoPanel::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
{
    if(mousePressed)
    {
        (isExpanded)? animHide() : animShow();
        e->accept();
    }

    mousePressed = false;

}

void CNoteInfoPanel::swipeGestureEvent (QGestureEvent *event, QSwipeGesture *gesture)
{
    qDebug() << "CNoteInfoPanel::swipeGestureEvent";

    if(gesture->state() == Qt::GestureFinished)
    {
        if(gesture->verticalDirection() == QSwipeGesture::Down)
        {
            event->accept(Qt::SwipeGesture);
            animShow();

        }
        else if(gesture->verticalDirection() == QSwipeGesture::Up)
        {
            event->accept(Qt::SwipeGesture);
            animHide();

        }
    }
}

/*void CNoteInfoPanel::panGestureEvent (QGestureEvent *event, QPanGesture *gesture)
{
    QRectF b = boundingRect();

    if(gesture->state() == Qt::GestureUpdated)
    {
        // Disable the press/release animation
        mousePressed = false;
        isExpanded = true;

        if(-b.height() + 90 + INFO_LINE_HEIGHT + gesture->offset().y() < 90)
            setGeometry(QRectF(0, -b.height() + 90 + INFO_LINE_HEIGHT + gesture->offset().y(), parentWidget()->geometry().width(), b.height()));
    }

    event->accept(Qt::PanGesture);
}*/

void CNoteInfoPanel::animHide()
{
    if(!isExpanded)
        return;

    qDebug() << "CNoteInfoPanel::animHide";

    QRectF b = boundingRect();
    panelAnim = new QPropertyAnimation(this, "geometry");
    panelAnim->setStartValue(b);
    panelAnim->setEndValue(QRectF(0, -b.height() + 90 + INFO_LINE_HEIGHT, parentWidget()->geometry().width(), b.height()));

    isExpanded = false;

    handleAnim = new QPropertyAnimation(&lLastTransparentLine, "opacity");
    handleAnim->setStartValue(0);
    handleAnim->setEndValue(1);

    panelAnim->start(QAbstractAnimation::DeleteWhenStopped);
    handleAnim->start(QAbstractAnimation::DeleteWhenStopped);
    //lLastTransparentLine.setText(lNoteTitle.text());
}

void CNoteInfoPanel::animShow()
{
    if(isExpanded)
        return;

    qDebug() << "CNoteInfoPanel::animShow";

    QRectF b = boundingRect();
    panelAnim = new QPropertyAnimation(this, "geometry");
    panelAnim->setStartValue(b);
    panelAnim->setEndValue(QRectF(0, parentWidget()->geometry().y() + 90, parentWidget()->geometry().width(), boundingRect().height()));

    handleAnim = new QPropertyAnimation(&lLastTransparentLine, "opacity");
    handleAnim->setStartValue(1);
    handleAnim->setEndValue(0);

    panelAnim->start(QAbstractAnimation::DeleteWhenStopped);
    handleAnim->start(QAbstractAnimation::DeleteWhenStopped);
    isExpanded = true;
    //lLastTransparentLine.setText("");
}

void CNoteInfoPanel::setNote(Note& note)
{
    Notebook notebook;

    //removeContent();

    if(QvernoteStorage::Instance()->isNoteFavorite(note.guid))
    {
            lFavorite.setPixmap(QPixmap(":/IconAdd/Star-full-48.png"));
            lFavorite.setProperty("isFavorite", QVariant(true));
    }
    else
    {
            lFavorite.setPixmap(QPixmap(":/IconAdd/Star-empty-48.png"));
            lFavorite.setProperty("isFavorite", QVariant(false));
    }

    lNoteTitle.setText(QString::fromUtf8(note.title.c_str()));

    if(m_hEvernote->getNotebookByGuid(notebook, note.notebookGuid))
            lNotebookName.setText(QString(trUtf8("In: ")) + QString::fromUtf8(notebook.name.c_str()));
    else
            lNotebookName.setText(trUtf8("Unknown notebook"));

    /* load tag names */
    vector<string>::iterator it;
    QString tagList = 0;
    for(it = note.tagNames.begin(); it < note.tagNames.end(); it++)
    {
            tagList += QString::fromUtf8((*it).c_str()) + " ";
    }

    if(tagList.size() == 0)
            lNoteTags.setText(trUtf8("No tags"));
    else
            lNoteTags.setText(tagList);

    lLastTransparentLine.setFont(QFont("Nokia", INFO_LINE_HEIGHT / 2));
    lLastTransparentLine.setText(lNoteTitle.text());
    //showContent();
}

void CNoteInfoPanel::removeContent()
{
    for(int i = 0; i < 4; i++)
        noteInfoPanelLayoutPolicy->removeAt(i);

}

void CNoteInfoPanel::setupUI()
{
    ciEmail.setBorderEnabled(false);
    ciEmail.setPixmap(QPixmap(":/IconAdd/general_email64.png"));
    ciEmail.setClickAnimEnabled(true);

    ciMap.setBorderEnabled(false);
    ciMap.setPixmap(QPixmap(":/IconAdd/Maps-blue-64.png"));
    ciMap.setClickAnimEnabled(true);

    //QObject::connect(ciEmail, SIGNAL(clicked()), SLOT(emailNote()));
    QObject::connect(&ciMap, SIGNAL(clicked()), qgraphicsitem_cast<QNoteViewWindow*>(parentWidget()), SLOT(showNoteMap()));

    noteInfoPanelLayoutPolicy->setSpacing(0);
    noteInfoPanelLayoutPolicy->addItem(&lFavorite, 1, 1);
    noteInfoPanelLayoutPolicy->addItem(&lNoteTitle, 1, 2);
    noteInfoPanelLayoutPolicy->addItem(&lNotebookName, 2, 2);
    noteInfoPanelLayoutPolicy->addItem(&lNoteTags, 3, 2);

    noteInfoPanelLayoutPolicy->addItem(&ciEmail, 4, 1);
    noteInfoPanelLayoutPolicy->addItem(&ciMap, 4, 2);

    noteInfoPanelLayoutPolicy->addItem(&lLastTransparentLine, 5, 1, 1, 2);
    noteInfoPanelLayoutPolicy->setRowFixedHeight(5, INFO_LINE_HEIGHT);
    //noteInfoPanelLayoutPolicy->setSpacing(0);
    //noteInfoPanelLayoutPolicy->setAlignment(&lLastTransparentLine, Qt::AlignVCenter|Qt::AlignLeft);
    //lLastTransparentLine.setAlignment(Qt::AlignVCenter|Qt::AlignLeft);
}
